import HeadPrefab from "../../component/HeadPrefab";
import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { ViewConfig } from "../config/ViewConfig";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewGiveGem extends ViewBase {

    private userData: object

    public onInitChild(userData: object) {
        this.userData = userData;
        this.refresh()
    }

    private refresh() {
        let head: HeadPrefab = this.getChild(this.node, "head", HeadPrefab)
        head.setHead(this.userData["headimg"], this.userData["lv"])
        let txtName: cc.Label = this.getChild(this.node, "txtName", cc.Label)
        txtName.string = this.userData["nick_name"]
        let txtId: cc.Label = this.getChild(this.node, "txtId", cc.Label)
        txtId.string = "ID:" + this.userData["userid"]

        let txtGiveNum: cc.EditBox = this.getChild(this.node, "txtGiveNum", cc.EditBox)

        let btnGive: cc.Node = this.findChild(this.node, "btnGive")
        UIHelper.onAddEvent(btnGive, () => {
            if (txtGiveNum.string == "") {
                UIHelper.showTip("请输入赠送数量")
                return;
            }
            let giveNum: number = parseInt(txtGiveNum.string)
            if (UserModel.instance.gems < giveNum) {
                UIHelper.showTip("您的钻石不足")
            } else {
                this.closeView()
                txtGiveNum.string = ""
                SocketClient.req_give_gems(this.userData["userid"], giveNum, ()=>{
                    
                })
            }
        })
     

    }

}
